<template>
  <div v-if="browserRedirect">
    <div>
      <div class="table-page-search-wrapper" v-show="showSizer">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :md="6" :sm="8">
              <a-form-item label="剧本名称筛选">
                <a-select defaultValue="" @change="changeSpecification" v-model="ftVal" style="width:100%;">
                  <a-select-option value="">请选择</a-select-option>
                  <a-select-option v-for="(val,index) in specification" :key="val.itemPatch" :value="val.id">{{val.productName+"/批次-"+val.itemPatch}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="8">
              <a-form-item label="绑定状态">
                <a-select defaultValue="" v-model="bindStatusVal" style="width:100%;">
                  <a-select-option value="">请选择</a-select-option>
                  <a-select-option v-for="(val,index) in bindList" :key="val.index" :value="val.index">{{val.name}}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-item label="店家手机">
                <a-input placeholder="" v-model="phone"></a-input>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="8">
              <a-form-item label="门店名称">
                <a-input placeholder="" v-model="shopkeeperName"></a-input>
              </a-form-item>
            </a-col>
            <!-- <a-col :md="4" :sm="8">
              <a-form-item label="批次">
                <a-input placeholder="" v-model="itemPatch"></a-input>
              </a-form-item>
            </a-col> -->
            <a-col :md="6" :sm="8">
              <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
                <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
                <a-button
                  type="primary"
                  @click="searchReset"
                  icon="reload"
                  style="margin-left: 8px"
                >重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <a-tabs default-active-key="1" size="large" :tab-bar-style="{marginBottom: '24px', paddingLeft: '16px'}">
        <div class="extra-wrapper" slot="tabBarExtraContent">
          <div class="selSty" @click="showSizer=!showSizer" style="line-height: 60px;">
            <span>筛选</span>
            <img style='margin-top: -3px;margin-left: 3px;' src="../../assets/common-icon-filter.svg">
          </div>
        </div>
        <a-tab-pane loading="true" tab="正版剧本绑定概览" key="1" style="min-height: 100vh;">
          <ul style="margin: 0;padding:0;list-style-type: none;">
            <li v-for="val in dataSource" class="liCommSty">
              <div style="border-bottom: 1px solid #EBEBEB;padding-bottom: 10px;">
                <div style="color: #333;font-weight: bold;">
                  <span>{{val.productName}}</span>
                  <span style="float: right;color:#FF5252;" v-if="val.bindStatus==0 || val.bindStatus==2">未绑定</span>
                  <span style="float: right;color:#00A580;" v-if="val.bindStatus==1">已绑定</span>
                </div>
                <div style="margin-top: 15px;color:#888;">
                  <span>批次:</span>
                  <span style="margin-left:10px;">{{val.itemPatch}}</span>
                </div>
              </div>
              <div v-if="val.bindStatus==1">
                <div style="margin-top: 10px;height:50px;">
                  <span style="color: #333;">店家头像</span>
                  <img style="float: right;width:50px;" :src="val.userAvatar">
                </div>
                <div style="margin-top: 10px;">
                  <span style="color: #333;">店家手机</span>
                  <span style="float: right;">{{val.phone}}</span>
                </div>
                <div style="margin-top: 10px;">
                  <span style="color: #333;">门店名称</span>
                  <span style="float: right;">{{val.shopkeeperName}}</span>
                </div>
                <div style="margin-top: 10px;">
                  <span style="color: #333;">门店地址</span>
                  <span style="float: right;">{{val.shopaddr}}</span>
                </div>
                <div style="margin-top: 10px;">
                  <span style="color: #333;">绑定时间</span>
                  <span style="float: right;">{{val.bindDatetime}}</span>
                </div>
              </div>
            </li>
          </ul>
          <a-pagination showQuickJumper :defaultCurrent="ipagination.current" :total="ipagination.total" @change="onChange"/>
        </a-tab-pane>
      </a-tabs>
    </div>
  </div>
  <a-card v-else :bordered="false" style="border-radius: 10px;">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="剧本名称筛选">
              <a-select defaultValue="" @change="changeSpecification" v-model="ftVal" style="width:100%;">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(val,index) in specification" :key="val.itemPatch" :value="val.id">{{val.productName+"/批次-"+val.itemPatch}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="绑定状态">
              <a-select defaultValue="" v-model="bindStatusVal" style="width:100%;">
                <a-select-option value="">请选择</a-select-option>
                <a-select-option v-for="(val,index) in bindList" :key="val.index" :value="val.index">{{val.name}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="店家手机">
              <a-input placeholder="" v-model="phone"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="4" :sm="8">
            <a-form-item label="门店名称">
              <a-input placeholder="" v-model="shopkeeperName"></a-input>
            </a-form-item>
          </a-col>
          <!-- <a-col :md="4" :sm="8">
            <a-form-item label="批次">
              <a-input placeholder="" v-model="itemPatch"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button
                type="primary"
                @click="searchReset"
                icon="reload"
                style="margin-left: 8px"
              >重置</a-button>
              <a-button type="primary" style="margin-left: 8px" v-if="dataSource.length>0" icon="download" @click="handleExportXls('二维码')">下载二维码</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button type="primary" v-if="dataSource.length>0" icon="download" @click="handleExportXls('二维码')">下载二维码</a-button>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div> -->

    <!-- table区域-begin -->
    <div>

      <a-table
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <div v-else @click="jumpProview(text,1)">
            <img :src="getImgView(text)" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </div>
        </template>
        <template slot="imgSlot1" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此图片</span>
          <div v-else @click="jumpProview(text,2)">
            <img :src="text" height="25px" alt="图片不存在" style="max-width:80px;font-size: 12px;font-style: italic;"/>
          </div>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无此文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="uploadFile(text)"
          >下载</a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <!-- <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" /> -->
          <a-popconfirm  v-if="record.bindStatus=='1'" title="确定解除绑定吗?" @confirm="() => handleUnbind(record.id)">
            <a>解除绑定</a>
          </a-popconfirm>
          
          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">
              更多
              <a-icon type="down" />
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>
    <JProviewImg v-if="showImg" @closeProview="closeProview" :imgurl="proviewUrl"></JProviewImg>
    <blockProductBindInfo-modal ref="modalForm" @ok="modalFormOk"></blockProductBindInfo-modal>
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { browserRedirect } from '@/utils/util'
import BlockProductBindInfoModal from './modules/BlockProductBindInfoModal'
import { initDictOptions, filterMultiDictText } from '@/components/dict/JDictSelectUtil'
import { getAction, putAction, postAction,downFile } from '@/api/manage'
import JProviewImg from '@/components/jeecg/JProviewImg'
import { ACCESS_TOKEN } from "@/store/mutation-types"
import Vue from 'vue'

export default {
  name: 'BlockProductBindInfoList',
  mixins: [JeecgListMixin],
  components: {
      BlockProductBindInfoModal,
      JProviewImg
  },
  data() {
    return {
      showSizer:false,
      bindStatusVal:"",
      browserRedirect:false,
      bindList:[
        {name:"未绑定",index:0},
        {name:"已绑定",index:1}
      ],
      ftVal:"",
      ipagination:{
        current: 1,
        pageSize: 10,
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        total: 0
      },
      headers:{},
      proviewUrl:"",
      showImg:false,
      specification:[],
      description: '剧本绑定信息管理页面',
      // 表头
      columns: [
        // {
        //   title: '发行方名称',
        //   align: 'center',
        //   dataIndex: 'manufacturerName'
        // },
        {
          title: '排序',
          align: 'center',
          dataIndex: 'sortNum'
        },
        {
          title: '剧本名称',
          align: 'center',
          dataIndex: 'productName'
        },
        {
          title: '批次',
          align: 'center',
          dataIndex: 'itemPatch'
        },
        {
          title: '绑定时间',
          align: 'center',
          dataIndex: 'bindDatetime'
        },
        {
          title: ' 绑定状态',
          align: 'center',
          dataIndex: 'bindStatus',
          // sortDirections: 'descend',
          // sorter: true,
          customRender: text => {
              return filterMultiDictText(this.dictOptions['bindStatus'], text + '')
          }
        },
        {
          title: '店家手机',
          align: 'center',
          dataIndex: 'phone'
        },
        // {
        //   title: '创建时间',
        //   align: 'center',
        //   dataIndex: 'createTime',
        //   customRender: function(text) {
        //     return !text ? '' : text.length > 10 ? text.substr(0, 10) : text
        //   }
        // },
        {
          title: '门店名称',
          align: 'center',
          dataIndex: 'shopkeeperName'
        },
        {
          title: '门店地址',
          align: 'center',
          dataIndex: 'shopaddr'
        },
        {
          title: '店家头像',
          align: 'center',
          dataIndex: 'userAvatar',
          scopedSlots: { customRender: 'imgSlot1' }
        },
        {
          title: '二维码',
          align: 'center',
          dataIndex: 'qrcodeImage',
          scopedSlots: { customRender: 'imgSlot' }
        }
        // {
        //   title: '初始绑定信息',
        //   align: 'center',
        //   dataIndex: 'initBindinfo'
        // },
      ],
      url: {
        list: '/block/blockProductBindInfo/list',
        delete: '/block/blockProductBindInfo/delete',
        deleteBatch: '/block/blockProductBindInfo/deleteBatch',
        exportXlsUrl: '/block/blockProductBindInfo/download',
        importExcelUrl: 'block/blockProductBindInfo/importExcel',
        unbindData: 'block/blockProductBindInfo/unBindData'
      },
      dictOptions: {
        bindStatus: []
      },
      productId:"",
      itemPatch:"",
      shopkeeperName:"",
      phone:"",
    }
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  mounted(){
    this.browserRedirect = browserRedirect();
    const token = Vue.ls.get(ACCESS_TOKEN);
    this.headers = {"X-Access-Token":token}
    this.indataList();
  },
  methods: {
    onChange(pageNumber) {
      this.ipagination.current = pageNumber;
      this.handleTableChange(this.ipagination,"","");
    },
    /* 图片预览 */
    getImgView(text){
      if(text && text.indexOf(",")>0){
        text = text.substring(0,text.indexOf(","))
      }
      return window._CONFIG['domianURL'] + '/sys/common/view'+text
    },
    searchReset(){
      this.itemPatch="";
      this.productId="";
      this.shopkeeperName="";
      this.phone="";
      this.bindStatusVal="";
      this.ipagination.current=1;
      this.searchQuery();
    },
    searchQuery(){
      var params={
        bindStatus:this.bindStatusVal,
        productStandardsId: this.productId,
        itemPatch:this.itemPatch,
        shopkeeperName:this.shopkeeperName,
        phone:this.phone,
        pageNo: this.ipagination.current,
        pageSize: 10
      };
      getAction("/block/blockProductBindInfo/list",params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    handleTableChange(pagination, filters, sorter){   
      // if(sorter.field){
      //   var orderName=sorter.order;
      //   var columnName=sorter.field;
      // }
      this.ipagination = pagination;
      var params={
        productStandardsId: this.productId,
        column:"createTime",
        // orderName:orderName,
        // columnName:columnName,
        bindStatus:this.bindStatusVal,
        itemPatch:this.itemPatch,
        shopkeeperName:this.shopkeeperName,
        phone:this.phone,
        order: "asc",
        field: "id,,,manufacturerName,productName,bindDatetime,bindStatus,phone,shopkeeperName,shopaddr,user_avatar,qrcodeImage,action",
        pageNo: this.ipagination.current,
        pageSize: 10
      };
      getAction("/block/blockProductBindInfo/list",params).then((res) => {
        if (res.success) {
          this.dataSource = res.result.records;
          this.ipagination.total = res.result.total;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    handleExportXls(fileName){
      if(!this.productId){
      this.$message.warning("请在剧本名称筛选中选择剧本！")
        return
      }        
      let param = {
        "id":this.productId
      }
      getAction("/block/blockProductBindInfo/preDownload",param).then((res) => {
        if (res.success) {
          if(res.result){
            downFile(this.url.exportXlsUrl,param).then((data)=>{
              console.log(data)
              if (!data) {
                this.$message.warning("文件下载失败")
                return
              }
              if (typeof window.navigator.msSaveBlob !== 'undefined') {
                window.navigator.msSaveBlob(new Blob([data]), fileName+'.zip')
              }else{
                let url = window.URL.createObjectURL(new Blob([data]))
                let link = document.createElement('a')
                link.style.display = 'none'
                link.href = url
                link.setAttribute('download', fileName+'.zip')
                document.body.appendChild(link)
                link.click()
                document.body.removeChild(link); //下载完成移除元素
                window.URL.revokeObjectURL(url); //释放掉blob对象
              }
            })
          }else{
            this.$message.warning(res.message)
          }
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    changeSpecification(value,option){
      this.itemPatch = option.key;
      this.productId = value;
      // var params={
      //   productStandardsId: value,
      //   column:"createTime",
      //   order: "asc",
      //   field: "id,,,manufacturerName,productName,bindDatetime,bindStatus,phone,shopkeeperName,shopaddr,user_avatar,qrcodeImage,action",
      //   pageNo: 1,
      //   pageSize: 10
      // };
      // getAction("/block/blockProductBindInfo/list",params).then((res) => {
      //   if (res.success) {
      //     this.dataSource = res.result.records;
      //     this.ipagination.total = res.result.total;
      //   }
      //   if(res.code===510){
      //     this.$message.warning(res.message)
      //   }
      // })
    },
    indataList(){
      var params={
        productStandardsId: "1216548480059949057",
        order: "asc",
        field: "",
        pageNo: 1,
        pageSize: 1000
      };
      getAction("/block/blockProductStandards/list",params).then((res) => {
        if (res.success) {
          this.specification = res.result.records;
        }
        if(res.code===510){
          this.$message.warning(res.message)
        }
      })
    },
    closeProview(){
      this.showImg = false;
    },
    jumpProview(val,num){
      this.showImg = true;
      if(num == 1){
        this.proviewUrl = window._CONFIG['domianURL'] + '/sys/common/view'+val;
      }else{
        this.proviewUrl = val;
      }
    },
    initDictConfig() {
      initDictOptions('bind_status').then(res => {
        if (res.success) {
          this.$set(this.dictOptions, 'bindStatus', res.result)
        }
      })
    },
    handleUnbind: function(id) {
      var params = {
        id: id
      }
      getAction(this.url.unbindData, params).then(res => {
        if (res.success) {
          
          this.$message.warning(res.message);
          this.loadData();
        }
        if (res.code === 510) {
          this.$message.warning(res.message)
        }
      })
    }
  },
  
}
</script>
<style scoped>
@import '~@assets/less/common.less';
.liCommSty{
    background-color: #fff;
    padding:15px;
    margin-bottom: 10px;
    border-radius: 6px;
  }
</style>